package com.mg.axechen.webview;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.mg.axechen.webview.databinding.ActivityBrowserBinding;


/**
 * @author AxeChen
 */
public class BrowserActivity extends AppCompatActivity {

    private static final String INTENT_KEY_URL = "intent_key_url";

    private static final String INTENT_KEY_TITLE = "intent_key_title";

    /**
     * dataBinding
     */
    private ActivityBrowserBinding mBrowserBinding;

    /**
     * webView
     */
    private BaseWebView mBaseWebView;

    /**
     * toolBar
     */
    private Toolbar mToolBar;

    /**
     * 网页标题
     */
    private String mTitle;

    /**
     * 网页URL
     */
    private String mUrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        getIntentData();
        loadUrl();
    }

    private void initView() {
        mBrowserBinding = ActivityBrowserBinding.inflate(getLayoutInflater());
        setContentView(mBrowserBinding.getRoot());

        mBaseWebView = mBrowserBinding.webView;
        mBaseWebView.init(new BaseWebViewConfig());

        initActionBar();
    }


    private void initActionBar() {
        mToolBar = mBrowserBinding.webViewToolBar;
        setSupportActionBar(mToolBar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void getIntentData(){
        Intent intent = getIntent();
        mTitle = intent.getStringExtra(INTENT_KEY_TITLE);
        mUrl = intent.getStringExtra(INTENT_KEY_URL);
    }

    private void loadUrl() {
        if (mBaseWebView != null) {
            mBaseWebView.loadUrl(mUrl);
        }
    }


    public static void startActivity(Context context, String title, String url) {
        Intent intent = new Intent(context, BrowserActivity.class);
        intent.putExtra(INTENT_KEY_TITLE, title);
        intent.putExtra(INTENT_KEY_URL, url);
        context.startActivity(intent);
    }


}
